package com.asset.dispoal.file.common;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.FillPatternType;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.springboot.protocol.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author qr
 * @date 2022/5/18$
 */
public class ExcelExport {

    public static <T> void export(File file, List<T> bsDataDetailExportTemplates, Class<T> clazz) {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setWrapped(false);
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 13);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        contentWriteCellStyle.setWrapped(false);
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 13);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 设定输出文件头，这里fileName=后面跟的就是文件的名称，可以随意更改
        //sheet()里面的内容是工作簿的名称
        EasyExcel.write(file).sheet("数据").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).head(clazz).doWrite(bsDataDetailExportTemplates);
    }

    public static <T> void export(HttpServletResponse response, String downloadFileName, List<T> bsDataDetailExportTemplates, Class<T> clazz) {
        try (OutputStream os = response.getOutputStream()) {
            // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setWrapped(false);
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 13);
            // 内容的策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            contentWriteCellStyle.setWrapped(false);
            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteFont.setFontHeightInPoints((short) 13);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 设定输出文件头，这里fileName=后面跟的就是文件的名称，可以随意更改
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(Optional.ofNullable(downloadFileName).orElse("下载文件") + ".xlsx", "UTF-8"));
            //sheet()里面的内容是工作簿的名称
            EasyExcel.write(os).sheet("案件信息").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).head(clazz).doWrite(bsDataDetailExportTemplates);
        } catch (IOException e) {
            throw new BusinessException(e);
        }
    }


    public static void exportTemplate(HttpServletResponse response, String downloadFileName, Class clazz) {
        try (OutputStream os = response.getOutputStream()) {
            // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setWrapped(false);
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 13);
            // 内容的策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            contentWriteCellStyle.setWrapped(false);
            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteFont.setFontHeightInPoints((short) 13);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 设定输出文件头，这里fileName=后面跟的就是文件的名称，可以随意更改
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(Optional.ofNullable(downloadFileName).orElse("模板文件") + ".xlsx", "UTF-8"));
            //sheet()里面的内容是工作簿的名称
            EasyExcel.write(os).sheet("案件信息").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).head(clazz).doWrite(data());
        } catch (IOException e) {
            throw new BusinessException(Result.SYS_FAILD, "导出失败", e);
        }
    }

    private static List<List<String>> data() {
        List<String> list1 = new ArrayList<>();
        list1.add("");
        List<List<String>> list2 = new ArrayList<>();
        list2.add(list1);
        return list2;
    }

}
